# 共用过滤器
from flask import session, current_app, g
import functools
from info.models import User


def do_index_class(index):

    if index == 0:
        return "first"
    elif index == 1:
        return "second"
    elif index == 2:
        return "third"
    else:
        return ""


def user_login_data(f):
    # 使用@functools.wraps(f)装饰内层函数，可以保持当前装饰的函数__name__（函数的名字）值不变
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        user_id = session.get('user_id')
        user = None  # type: User
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)

        # 把查询出的值赋值给g变量
        g.user = user
        return f(*args, **kwargs)
    return wrapper